package com.hxkcloud.patient.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxkcloud.patient.R;


/**
 * @ClassName: LoadingDialog.java
 * @Description:
 */

public class LoadingDialog extends Dialog {

    private Context mContext;
    private TextView tv_loading;
    private ImageView img_loading;
    // 均匀旋转动画
//	private RotateAnimation loadingAnimation;
    private AnimationDrawable animDrawable;

    public LoadingDialog(Context context) {
        super(context, R.style.loading_dalog);
        mContext = context;
        init();
    }

    public LoadingDialog(Context context, int theme) {
        super(context, R.style.loading_dalog);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_loading, null);

        tv_loading = (TextView) v.findViewById(R.id.tv_loading);
        img_loading = (ImageView) v.findViewById(R.id.img_loading);

//		loadingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(
//				mContext, R.anim.rotating);
//		// 添加匀速转动动画
//		LinearInterpolator lir = new LinearInterpolator();
//		loadingAnimation.setInterpolator(lir);
//		img_loading.startAnimation(loadingAnimation);

//        animDrawable = new AnimationDrawable();
//        animDrawable.setOneShot(false);
//        animDrawable.addFrame(ContextCompat.getDrawable(mContext, R.drawable.loadinganim_1), 200);
//        animDrawable.addFrame(ContextCompat.getDrawable(mContext, R.drawable.loadinganim_2), 200);
//        animDrawable.addFrame(ContextCompat.getDrawable(mContext, R.drawable.loadinganim_3), 200);
//        animDrawable.addFrame(ContextCompat.getDrawable(mContext, R.drawable.loadinganim_4), 200);
//        animDrawable.addFrame(ContextCompat.getDrawable(mContext, R.drawable.loadinganim_5), 200);
//        img_loading.setImageDrawable(animDrawable);
//        animDrawable.start();

        animDrawable = (AnimationDrawable) ContextCompat.getDrawable(mContext, R.anim.anim_nurse);
        img_loading.setImageDrawable(animDrawable);
        animDrawable.start();

        this.setContentView(v);
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
    }

    public void setLoadingText(String text) {
        tv_loading.setText(text);
    }

    public void setLoadingText(int text) {
        tv_loading.setText(text);
    }

    public TextView getLoadingText() {

        return tv_loading;
    }

    @Override
    public void dismiss() {
        if (mContext == null || ((Activity) mContext).isFinishing()) {
            return;
        }
        try
        {
            img_loading.clearAnimation();
            super.dismiss();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void show() {
        try {
            if (mContext == null || ((Activity) mContext).isFinishing()) {
                return;
            }
            animDrawable.start();
            super.show();
        } catch (Exception e) {
        }
    }

}
